package com.newhero.product.module.sys.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.newhero.product.common.mybatis.mapper.EntityWrapper;
import com.newhero.product.common.mybatis.plugins.Page;
import com.newhero.product.common.mybatis.plugins.pagination.Pagination;
import com.newhero.product.common.service.impl.BaseServiceImpl;
import com.newhero.product.common.util.StringUtils;
import com.newhero.product.module.sys.mapper.UserMapper;
import com.newhero.product.module.sys.mapper.UserRoleMapper;
import com.newhero.product.module.sys.model.User;
import com.newhero.product.module.sys.model.UserRole;
import com.newhero.product.module.sys.model.vo.UserVo;
import com.newhero.product.module.sys.service.IUserService;


/**
 *
 * User 表数据服务层接口实现类
 *
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    
    public List<User> selectByLoginName(UserVo userVo) {
        User user = new User();
//        user.setLoginName(userVo.getLoginName());
        user.setUserLoginName(userVo.getUserLoginName());
        EntityWrapper<User> wrapper = new EntityWrapper<User>(user);
        if (null != userVo.getUserID()) {
            wrapper.where("id != {0}", userVo.getUserID());
        }
        return this.selectList(wrapper);
    }




    @Override
    public void updatePwdByUserId(String userId, String md5Hex) {
        User user = new User();
        user.setUserID(userId);
        user.setUserPwd(md5Hex);
        this.updateById(user);
    }

    @Override
    public void deleteUserById(String id) {
        this.deleteById(id);
        List<UserRole> userRoles = userRoleMapper.selectByUserId(id);
        if (userRoles != null && !userRoles.isEmpty()) {
            for (UserRole userRole : userRoles) {
                userRoleMapper.deleteById(userRole.getUrID());
            }
        }
    }

    /*
     * (non-Javadoc)
     * @see com.newhero.product.module.sys.service.IUserService#checkIfUserLoginNameExsit(java.lang.String, java.lang.String)
     */
	@Override
	public boolean checkIfUserLoginNameExsit(String userID, String userLoginName) {
		/*
		 * 如果userID存在，则要排除他的用户名
		 */
		int result = 1;
		if(StringUtils.isNotBlank(userID)){
			result = userMapper.checkIfUserLoginNameExist(userID, userLoginName);
		}else{
			result = userMapper.checkIfUserLoginNameExist(null, userLoginName);
		}
		if(result >= 1){
			return false;
		}else{
			return true;
		}
		
	}




	@Override
	public List<UserVo> selectUserVoPage(Pagination page,
			Map<String, Object> params) {
		
		return userMapper.selectUserVoPage(page, params);
	}



	/*
	 * (non-Javadoc)
	 * @see com.newhero.product.module.sys.service.IUserService#selectUserListPage(com.newhero.product.common.mybatis.plugins.Page, java.util.Map)
	 */
	@Override
	public List<UserVo> selectUserListPage(
			Page<UserVo> dataPage, Map<String, Object> params) {
		
		return userMapper.selectUserListPage(dataPage, params);
	}


}